package cn.szv5.register.client;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * 应用模块名称<p> 代码描述<p> Copyright: Copyright (C) 2022 , Inc. All rights reserved. <p> Company: 阿飞工作室<p>
 *
 * @author Carl
 * @since 2022/2/7 18:45
 */
public class HttpSender {
    /**
     * 发送注册请求
     *
     * @param request
     * @return
     */
    public RegisterResponse register(RegisterRequest request) {
        System.out.println("服务实例【" + request + "】,发送请求进行注册......");

        RegisterResponse response = new RegisterResponse();
        response.setStatus(RegisterResponse.SUCCESS);

        return response;
    }

    /**
     * 发送心跳请求
     *
     * @param request
     * @return
     */
    public HeartbeatResponse heartbeat(HeartbeatRequest request) {
        System.out.println("服务实例【" + request + "】,发送心跳请求......");

        HeartbeatResponse response = new HeartbeatResponse();
        response.setStatus(HeartbeatResponse.SUCCESS);

        return response;
    }

    /**
     * 全量拉取服务注册表
     *
     * @return
     */
    public Applications fetchFullRegistry() {
        Map<String, Map<String, ServiceInstance>> registry = new HashMap<String, Map<String, ServiceInstance>>();
        Map<String, ServiceInstance> serviceInstances = new HashMap<>();

        ServiceInstance serviceInstance = new ServiceInstance();
        serviceInstance.setHostname("finance-service-01");
        serviceInstance.setIp("192.168.31.207");
        serviceInstance.setPort(9000);
        serviceInstance.setServiceInstanceId("FINANCE-SERVICE-192.168.31.207:9000");
        serviceInstance.setServiceName("FINANCE-SERVICE");

        serviceInstances.put("FINANCE-SERVICE-192.168.31.207:9000", serviceInstance);

        registry.put("FINANCE-SERVICE", serviceInstances);

        System.out.println("拉取注册表：" + registry);
        return new Applications(registry);
    }

    public DeltaRegistry fetchDeltaRegistry() {
        LinkedList<CachedServiceRegistry.RecentlyChangedServiceInstance> recentlyChangedQueue = new LinkedList<>();
        ServiceInstance serviceInstance = new ServiceInstance();
        serviceInstance.setHostname("order-service-01");
        serviceInstance.setIp("192.168.31.288");
        serviceInstance.setServiceInstanceId("ORDER-SERVICE-192.168.31.288:9000");
        serviceInstance.setServiceName("ORDER-SERVICE");

        CachedServiceRegistry.RecentlyChangedServiceInstance recentlyChangedItem =
            new CachedServiceRegistry.RecentlyChangedServiceInstance(serviceInstance, System.currentTimeMillis(),
                CachedServiceRegistry.ServiceInstanceOperation.REGISTER);

        recentlyChangedQueue.add(recentlyChangedItem);
        System.out.println("拉取增量注册表：" + recentlyChangedQueue);
        DeltaRegistry deltaRegistry = new DeltaRegistry(recentlyChangedQueue, 2L);
        return deltaRegistry;
    }

    /**
     * 服务下线
     *
     * @param serviceName
     * @param serviceInstanceId
     */
    public void cancel(String serviceName, String serviceInstanceId) {
        System.out.println("服务实例【" + serviceName + ", " + serviceInstanceId + "】下线");
    }
}
